package jgl.vajra.management.part.vehicle.pop;

import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import jgl.vajra.management.R;
import jgl.vajra.management.databinding.PopDocumentTypeBinding;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.utils.UIUtils;

public class DocumentTypePop {
    private FragmentActivity activity;
    private View mPopRootView;
    private PopupWindow menuPop;

    public DocumentTypePop(FragmentActivity activity) {
        this.activity = activity;
    }


    private void initPopupWindow() {
        mPopRootView = View.inflate(UIUtils.getContext(), R.layout.pop_document_type, null);
        PopDocumentTypeBinding mBinding = DataBindingUtil.bind(mPopRootView);

        menuPop = new PopupWindow(mPopRootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        menuPop.setOutsideTouchable(false);
        menuPop.setBackgroundDrawable(new BitmapDrawable());
        menuPop.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        menuPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //popupwindow消失的时候恢复成原来的透明度
                backgroundAlpha(1f);
            }
        });
        mBinding.setView(this);


        List<String> list=new ArrayList<>();
        list.add("驾驶证");
        list.add("行驶证");
        list.add("特种设备作业人员证");
        list.add("场(厂)内专用车辆合格证");
        list.add("技术等级证");
        list.add("运营证");
        list.add("随车吊操作证");
        list.add("高处作业操作证");
        list.add("特种设备检验合格证");
        list.add("上岗证");

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(activity, list, R.layout.item_pop_document_type);
        mBinding.recyclerView.setAdapter(adapter);

    }

    public void showPopupWindow(View v) {
        if (menuPop == null)
            initPopupWindow();
        int[] location = new int[2];
        v.getLocationOnScreen(location);  //获取控件的位置坐标
        //获取自身的长宽高
        mPopRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        backgroundAlpha(0.5f);
        menuPop.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    public boolean isShowing() {
        return menuPop != null && menuPop.isShowing();
    }

    public void dismiss() {
        if (menuPop != null && menuPop.isShowing())
            menuPop.dismiss();
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if (activity == null) return;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);

    }

}
